<template>
  <div class="topicContentModule main" v-if="topicData.length > 0">
    <div class="BreadcrumbBox">
      <p> <span class="active" @click="toActive">话题列表</span> <span class="ic">/</span> <span>话题详情</span> </p>
    </div>
    <div class="left">
      <div class="topic-wrap">
        <div class="topicTag">
          <a href="javascript:void(0);" class="tag">{{ topicData[0].tagName }}</a>
        </div>
        <div class="topicBox">
          <div class="title">{{ topicData[0].title }}</div>
          <div class="topicInfo clearfix">
            <div class="postTime" v-html="topicData[0].createTime"></div>
            <div class="viewTotal-icon">
              <i class="el-icon-view"></i>
            </div>
            <div class="viewTotal">{{ topicData[0].views }}次阅读</div>
            <div class="comment-icon">
              <i class="el-icon-chat-dot-round"></i>
            </div>
            <div class="comment">
              {{topicData[0].commentTotal}}个评论
            </div>
          </div>
          <!-- topicId属性用来支持JS获取隐藏内容 -->
          <div class="topicContent" v-html="topicData[0].detail"></div>
        </div>
      </div>

      <div class="commentModule" v-if="checkTagType()">
        <ol class="commentList">
          <div class="commentInfo" v-for="(item, index) in commentData" :key="index" v-show="item.status === '1'">
            <div class="comment-author">
              <a href="javascript:void(0);">
                <img v-if="item.real === '1'" :src="item.src || require('src/assets/imgs/use.png')" />
                <img v-else :src="require('src/assets/imgs/use.png')" />
                <cite> </cite></a><a href="javascript:void(0);">
                <span v-if="item.real === '1'">{{ item.nickName }}</span>
                <span v-else>匿名用户</span>
              </a>
              <div v-show="item.userId === topicData[0].userId && item.real === '1'&& topicData[0].real === '1'" class="userTag" style="display: inline-block; margin-left: 5px">
                <span class="master">作者</span>
              </div>
            </div>
            <div class="commentTime">{{ item.createTime }}</div>
            <div class="commentContent" v-html="item.detail"></div>
            <div class="clearfix"></div>
            <div class="replyList" v-if="item.reply.total > 0">
              <ul class="box">
                <li v-for="(itemG, i) in item.reply.Datas" :key="i">
                  <div class="reply-top">
                    <a class="avatarBox" href="javascript:void(0);">
                      <img v-if="itemG.real === '1'" :src="itemG.src || require('src/assets/imgs/use.png')" />
                      <img v-else :src="require('src/assets/imgs/use.png')" />
                    </a>
                    <a class="userName" href="javascript:void(0);">
                      <span v-if="itemG.real === '1'">{{ itemG.nickName }}</span>
                      <span v-else>匿名用户</span>
                    </a>
                    <div class="userTag" v-show="itemG.userId === topicData[0].userId && itemG.real === '1'&& topicData[0].real === '1'">
                      <span class="master">作者</span>
                    </div>
                    <span class="time">
                      {{ itemG.createTime }}
                    </span>
                  </div>
                  <div style="
                                            clear: both;
                                            height: 0;
                                            line-height: 0;
                                            font-size: 0;
                                        "></div>
                  <div id="editCommentReply-replyContent_1" class="replyContent" v-html="itemG.detail"></div>
                  <div id="editCommentReply-post_1"></div>
                </li>
              </ul>
            </div>
            <div v-if="item.reply.total>5 && !item.replyShow">
              <p style="color:#707070">还有{{item.reply.total-5}}条回复，<span style="color:#2d64b3" @click="item.replyShow = true">点击查看</span> </p>
            </div>
            <el-pagination v-if="item.reply.total>5 && item.replyShow" layout="prev, pager, next" @size-change="e => handleSizeChangeReply(e, item.id,index)" @current-change="e => handleCurrentChangeReply(e, item.id,index)" :pageNo="item.reply.pageNo" :pageSize="item.reply.pageSize" :total="item.reply.total">
            </el-pagination>
            <div id="commentReply-post_5" class="commentReply-post" v-if="item.show">
              <el-input type="textarea" :rows="7" placeholder="写下您的回复" v-model="searchItem.commentDetail"> </el-input>
              <div style="margin: 10px 0">
                <el-form :inline="true">
                  <el-form-item style="margin-bottom: 0px" label="">
                    <el-button type="primary" size="small" @click="add(true,item,index)">
                      <span style="font-size: 16px">提交</span>
                    </el-button>
                    <el-button type="default" size="small" @click="commentShow(item, false,index)">
                      <span style="font-size: 16px;color: #30aff7;">取消</span>
                    </el-button>
                  </el-form-item>
                  <el-form-item style="margin-bottom: 0px" label="是否匿名" prop="real">
                    <el-radio-group v-model="searchItem.real">
                      <el-radio :label="0">是</el-radio>
                      <el-radio :label="1">否</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-form>
              </div>
            </div>
            <div id="quote-post_5" class="quote-post"></div>
            <div id="editComment-post_5" class="editComment-post"></div>
            <div class="tipBottom" v-if="topicData[0].status == '2'">
              <span class="a-toolbar">
                <a href="javascript:void(0);"  @click="commentShow(item, true)">回复</a>
              </span>
            </div>
          </div>
        </ol>
        <div class="commentPage" >
          <pagination @size-change="handleSizeChange" :page-sizes="[5, 10]" @current-change="handleCurrentChange" :pageNo="pager.pageNo" :pageSize="pager.pageSize" :total="pager.total"> </pagination>
        </div>
      </div>

      <div class="addCommentModule" v-if="topicData[0].status == '2'">
        <div class="addComment-wrap" v-if="!userInfo.secret">
          <div class="respond">
            <p>
              请用员工账号
              <a href="javascript:void(0);" @click="toLogin">登录</a>
              后评论。
            </p>
          </div>
        </div>
        <div class="comment-formModule" v-else-if="checkTagType()">
          <div class="addComment-wrap">
            <editor ref="editorEl" @editChange="getEdit"></editor>
            <el-row style="margin-top: 10px">
              <el-col :span="12">
                <el-form :inline="true" :model="searchItem">
                  <el-form-item style="margin-bottom: 0px" label="">
                    <el-button type="primary" size="small" @click="add(false)">
                      <span style="
                                                    font-size: 18px;
                                                    padding: 0 10px;
                                                ">提交</span>
                    </el-button>
                  </el-form-item>

                  <el-form-item style="margin-bottom: 0px" label="是否匿名" prop="real">
                    <el-radio-group v-model="searchItem.real">
                      <el-radio :label="0">是</el-radio>
                      <el-radio :label="1">否</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-form>
              </el-col>
              <el-col :span="12">
                <p style="
                                        line-height: 40px;
                                        font-size: 16px;
                                        text-align: right;
                                        color: #ccc;
                                    ">
                  我来说两句，发表我的评论
                </p>
              </el-col>
            </el-row>
          </div>
        </div>
      </div>
    </div>

    <div class="right">
      <div class="userInfo-wrap clearfix">
        <div class="userInfo">
          <div class="author">
            <a href="javascript:void(0);">
              <img v-if="topicData[0].real === '1'" :src="topicData[0].src || require('src/assets/imgs/use.png')" />
              <img v-else :src="require('src/assets/imgs/use.png')" />
            </a>
          </div>
          <p class="name">
            <a href="javascript:void(0);">
              <span v-if="topicData[0].real === '1'">{{ topicData[0].nickName }}</span>
              <span v-else>匿名用户</span>
            </a>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import {
    ref, reactive, onMounted, computed, Ref
} from 'vue';
import userHooks from 'src/hooks/userHooks';
import store from 'src/store';
import {
    getArticle,
    addComment,
    addReply,
    getComment,
    getReply
} from 'src/plugins/http/server/article/index';
import { useRoute, useRouter } from 'vue-router';
import BetterUtilTools from 'better-util-tools';
import { msgFn } from 'src/utils';
import editor from '@/components/editor/index.vue';
import pagination from '@/components/pagination/index.vue';

const UtilTools = new BetterUtilTools();

export default {
    'components': {
        pagination,
        editor
    },
    setup() {
    // 初始化分页对象
        const pager = reactive({
            'pageNo': 1,
            'pageSize': 5,
            'total': 0
        });

        const searchItem = reactive({
            'commentDetail': '',
            'targetId': '',
            'real': 1
        });
        const { tagData } = store.state;
        const $route = useRoute();
        const $router = useRouter();
        const { toLogin } = userHooks({});
        const userInfo = computed(() => store.state.userInfo);
        const { topicId } = $route.params;
        const commentData: Ref<commentObj[]> = ref([]);
        const topicData: Ref<articleObj[]> = ref([]);
        const editorEl = ref();
        const getTopic = () => {
            let params = {
                'self': $route.query.self === '1',
                'articleId': topicId
            };
            getArticle({
                'data': params
            })
                .then((res: any) => {
                    res.data.articleDatas.forEach((item: articleObj) => {
                        item.createTime = UtilTools.formatDateTime({
                            'date': item.createTime
                        });
                        item.modifyTime = UtilTools.formatDateTime({
                            'date': item.modifyTime
                        });
                        item.tagName = tagData.filter(
                            tag => tag.id === item.tagId
                        )[0]?.value;
                    });
                    topicData.value = res.data.articleDatas;
                })
                .catch(e => {
                    console.log(e);
                });
        };

        const checkTagType = () => {
            if (
                topicData.value[0].tagId === '33a18364a759436aaab2dedb6b2a336c' ||
        topicData.value[0].tagId === 'c15add25-f6eb-43a1-ac00-05759e008415'
            ) {
                return false;
            }
            return true;
        };

        const getCommentAll = () => {
            let params = {
                'pageNo': pager.pageNo,
                'pageSize': pager.pageSize,
                'status':'1',
                'id': topicId
            };
            getComment({
                'data': params
            })
                .then((res: any) => {
                    res.data.Datas.forEach((item: commentObj) => {
                        item.show = false;
                        item.reply.pageNo = 1;
                        item.reply.pageSize = 5;
                        item.show = false;
                        item.replyShow = false;
                        item.createTime = UtilTools.formatDateTime({
                            'date': item.createTime
                        });
                        if (item.reply.total > 0) {
                            item.reply.Datas.forEach((element: replyObj) => {
                                element.createTime = UtilTools.formatDateTime({
                                    'date': element.createTime
                                });
                            });
                        }
                    });
                    pager.total = res.data.total;
                    commentData.value = res.data.Datas;
                })
                .catch(e => {
                    console.log(e);
                });
        };

        const toActive = () => {
            if (store.state.code) {
                $router.push(`/articleList/${store.state.code}`);
            } else {
                $router.push('/articleList/1');
            }
        };

        const getReplyAll = (id: string, i: number) => {
            let params = {
                'pageNo': commentData.value[i].reply.pageNo,
                'pageSize': commentData.value[i].reply.pageSize,
                'status':'1',
                'id': id
            };
            getReply({
                'data': params
            })
                .then((res: any) => {
                    res.data.Datas.forEach((item: commentObj) => {
                        item.createTime = UtilTools.formatDateTime({
                            'date': item.createTime
                        });
                    });
                    commentData.value[i].reply.Datas = res.data.Datas;
                })
                .catch(e => {
                    console.log(e);
                });
        };
        const getEdit = (html: string) => {
            searchItem.commentDetail = html;
        };
        //切换页面大小方法
        const handleSizeChangeReply = (val: number, id: string, i: number) => {
            commentData.value[i].reply.pageSize = val;
            commentData.value[i].reply.pageNo = 1;
            getReplyAll(id, i);
        }; //分页方法
        const handleCurrentChangeReply = (val: number, id: string, i: number) => {
            commentData.value[i].reply.pageNo = val;
            getReplyAll(id, i);
        };

        const add = (type: boolean, item: any, index: number) => {
            if (searchItem.commentDetail) {
                let params: addCommentOrDetail = {
                    'detail': searchItem.commentDetail,
                    'real': searchItem.real.toString()
                };
                let addFn;
                if (type) {
                    params.commentId = item.id;
                    params.targetId = item.userId;
                    addFn = addReply;
                } else {
                    params.articleId = topicId;
                    params.targetId = topicData.value[0].userId;
                    addFn = addComment;
                }
                addFn({
                    'data': params
                })
                    .then((res: any) => {
                        if (res.code === 200) {
                            msgFn('发布成功', 'success');
                            searchItem.commentDetail = '';
                            searchItem.real = 1;
                            editorEl.value.insert('');
                            if (type) {
                                getCommentAll();
                                handleCurrentChangeReply(item.reply.pageNo, item.id, index);
                            } else {
                                getCommentAll();
                            }
                        }
                    })
                    .catch((e:any) => {
                        console.log(e);
                    });
            }
        };
        //切换页面大小方法
        const handleSizeChange = (val: number) => {
            pager.pageSize = val;
            pager.pageNo = 1;
            getCommentAll();
        }; //分页方法
        const handleCurrentChange = (val: number) => {
            pager.pageNo = val;
            getCommentAll();
        };

        onMounted(() => {
            getTopic();
            getCommentAll();
        });

        const commentShow = (item: commentObj, type: boolean, index: number) => {
            if (userInfo.value.id) {
                commentData.value.forEach((g: commentObj) => {
                    g.show = false;
                });
                item.show = type;
            } else {
                msgFn('请登录');
            }
        };
        return {
            pager,
            editorEl,
            userInfo,
            searchItem,
            topicData,
            commentData,
            toActive,
            toLogin,
            commentShow,
            add,
            getEdit,
            handleSizeChange,
            handleCurrentChange,
            handleSizeChangeReply,
            handleCurrentChangeReply,
            checkTagType
        };
    }
};
</script>
()
<style lang="less" scoped>
@import '~@/views/article/detail/index.less';
</style>
